/*
 * @FileName: [CompanyController.java] 
 * @Package com.bci.pwtz.controller 
 * 
 * 
 * Copyright (c) 2011-2015 BCINFO Technology Limited Com.
 * All rights reserved.
 * 
 * This software is the confidential and proprietary 
 * information of BCINFO Technology Limited Company
 * ("Confidential Information"). You shall not disclose 
 * such Confidential Information and shall use it only
 * in accordance with the terms of the contract agreement 
 * you entered into with RKY.
 * 
 * $Rev: 2086 $
 * $LastChangedDate$
 * $LastChangedBy: lengzq $
 * 
 * @category ALUMNI
 * @version 1.1
 * @author $Author: lengzq $
 *
 * Change History:[Formatter: author date description] <br/>
 * 1
 * 2
 * 3
*/


package com.bci.pwtz.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.common.ActionCodeConstants;
import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.CompanyRoleActionMapper;
import com.bci.pwtz.mysql.model.BusinessOperateLog;
import com.bci.pwtz.mysql.model.CompanyRole;
import com.bci.pwtz.mysql.model.CompanyRoleAction;
import com.bci.pwtz.mysql.model.CompanyUser;
import com.bci.pwtz.service.BusinessOperateLogService;
import com.bci.pwtz.service.CompanyRoleService;

/**
 * 一句话描述 <br/>
 * <p>Description:<br/>
 *    <p>TODO 详细描述
 *
 * <p>For Examples <br/>
 * <PRE>
 *      TODO 代码使用示例
 * </PRE>
 * <p>
 */
@Controller
@RequestMapping("user/log")
public class BusinessOperateLogController extends AbstractController
{
    @Autowired
    private BusinessOperateLogService businessOperateLogService;
    @Autowired
    private CompanyRoleService roleService;
    @Autowired
    private CompanyRoleActionMapper actionMapper;
    @RequestMapping(value="/list",method=RequestMethod.POST)
    @ResponseBody
    public PaginationList<BusinessOperateLog>  serachLogs(int operationType,int deviceType,String beginTime, String endTime,String userName,int pageNo){
        PaginationList<BusinessOperateLog> list = null;
        CompanyUser user = (CompanyUser)session.getAttribute("user");
        
        try
        {
            List<CompanyRole> roles=roleService.findCompanyRoleByUserId(user.getCompanyUserId());
            boolean flag=false;
            for(CompanyRole r:roles){
                List<CompanyRoleAction> actions=actionMapper.selectByRoleId(r.getCompanyRoleId());
                for(CompanyRoleAction a:actions){
                    if(a.getActionCode().equals(ActionCodeConstants.COMPANY_USER_OPERATE_LOG_QUERY)){
                        flag=true;
                        break;
                    }
                }
                if(flag){
                    break;
                }
            }
            if(flag){
                list=businessOperateLogService.findLogs(operationType, deviceType,user.getLastCompanyId(), beginTime, endTime, null, userName, getPageSize(), pageNo);
            }else{
                list=businessOperateLogService.findLogs(operationType, deviceType,user.getLastCompanyId(), beginTime, endTime, user.getCompanyUserId(), userName, getPageSize(), pageNo);
            }
        }
        catch (PwtzDatabaseException e)
        {
            e.printStackTrace();
        }
        return list;
    }
}
